#!/bin/sh
#
# $Header: mkinitramfs-ll/scripts/suspend                Exp $
# $Author: (c) 2011-2014 -tclover <tokiclover@gmail.com> Exp $
# $License: 2-clause/new/simplified BSD                  Exp $
# $Version: 0.14.7 2014/11/11 12:33:03                   Exp $
#

pkg=${0##*/}

usage() {
	cat <<-EOH
 usage: $pkg [OPTIONS] [(toi) file/swap | (swsusp) major:minor]
   -o, --option platform|shutdown    Set swsusp suspend mode (ACPI/shutdown)
   -m, --method [mem|disk]|[3|4|5]   Set suspend method (ACPI S2/4/5)
                First set for swsusp; Second set for TuxOnIce
   -c, --compression <comp>          Set TuxOnIce compression algorithm
   -s, --size N                      Set image size limit to N bytes
   -t, --tuxonice                    use ToI hibernation implementation
   -H, --hibernate                   Foricibly hibernate at the end
   -h, --help                        Print this help message
EOH
}

while [ $# -ge 1 ]; do
	case "$1" in
		(-c|--compression)
			comp=$2;
			shift 2;;
		(-m|--method)
			method=$2
			shift 2;;
		(-o|--option)
			option=$2
			shift 2;;
		(-s|--size)
			size=$2
			shift 2;;
		(-t|--tuxonice)
			tuxonice=true
			shift 2;;
		(-H|--hibernate)
			hibernate=true
			shift;;
		(-h|--help)
			usage
			exit;;
	esac
done

if [ $# != 1 ]; then
	echo "$pkg: missing required argument" >&2
	exit 1
fi

if [ -n "$tuxonice" ]; then
	if [ ! -d /sys/power/tuxonice ]; then
		echo "$pkg: Error: TuxOnIce hibernation is not supported" >&2
		exit 2
	fi
	[ -n "$comp" ] && echo $size > /sys/power/tuxonice/compression/algorithm
	[ -n "$size" ] && echo $size > /sys/power/tuxonice/image_size_limit
	echo ${method:-4} > /sys/power/tuxonice/powerdown_method
	echo $1 > /sys/power/tuxonice/swap/swapfile
	echo "Resume device: (ToI) $(cat /sys/power/tuxonice/swap/headerlocations)"
	cmd='echo > /sys/power/tuxonice/do_hibernate'
else
	[ -n "$size" ] && echo $size > /sys/power/image_size
	echo $1 > /sys/power/resume
	echo ${option:-platform}  > /sys/power/disk
	cmd='echo ${method:-disk} > /sys/power/state'
fi

[ -n "$hibernate" ] && eval $cmd

unset cmd comp hibernate method size tuxonice

#
# vim:fenc=utf-8:ci:pi:sts=0:sw=4:ts=4:
#
